﻿using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Panda.DynamicWebApi;
using Panda.DynamicWebApi.Attributes;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WareHose.Application.allots.Dto;
using WareHose.Application.breakageS.Dto;
using WareHouse.Domain;
using WareHouse.Repositories;

namespace WareHose.Application.breakageS
{
    [DynamicWebApi]
    [ApiExplorerSettings(GroupName = "v2")]
    public class breakageService:IDynamicWebApi
    {
        //仓储
        private readonly IBaseRepository<breakage> db;
        //AutoMapper
        private readonly IMapper mapper;
        private readonly ISqlSugarClient sqlSugarClient;


        public breakageService(IBaseRepository<breakage> db, IMapper mapper, ISqlSugarClient sqlSugarClient)
        {
            this.db = db;
            this.mapper = mapper;
            this.sqlSugarClient = sqlSugarClient;
        }




        /// <summary>
        /// 报损添加
        /// </summary>
        /// <param name="RoleInputDTo"></param>
        /// <returns></returns>

        [HttpPost]
        [AllowAnonymous]
        public async Task<ResultDto> AddUpdAsync(breakageSDto breakageSDto)
        {
            try
            {

                var entity = mapper.Map<breakageSDto, breakage>(breakageSDto);


                await db.InsertAsync(entity);
                return new ResultDto { Code = Status.Success, Message = "添加成功" };

            }
            catch (Exception e)
            {
                return new ResultDto { Code = Status.Failed, Message = e.Message };
            }
        }



        /// <summary>
        ///报损显示
        /// </summary>
        /// <param name="RoleRequestDTo"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultDtos<List<breakageSDto>>> AdminPage(string? Bod, string breakageType, int State, int pageIndex = 1, int pageSize = 4)
        {

            var list = (from a in await db.QueryAsync()
                        select new breakageSDto
                        {
                            BId = a.BId,
                            Bod = a.Bod,
                            BName = a.BName,
                            Bserial = a.Bserial,
                            Bbatch = a.Bbatch,
                            Bguige = a.Bguige,
                          
                            NUmber = a.NUmber,
                            Cang = a.Cang,
                            Che = a.Che,
                            breakageChe = a.breakageChe,
                            breakageType = a.breakageType,
                            State = a.State,
                            auditor = a.auditor,
                            auditorDate = a.auditorDate,
                        });
            if (!string.IsNullOrEmpty(Bod))
            {
                list = list.Where(m => m.Bod.Contains(Bod));
            }
            if (breakageType != null)
            {
                list = list.Where(m => m.breakageType == breakageType);
            }
            if (State != 0)
            {
                list = list.Where(m => m.State == State);
            }

            int listcount = list.Count();
            list = list.OrderBy(x => x.BId).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

            return new ResultDtos<List<breakageSDto>>
            {
                Code = Status.Success,
                Data = list.ToList(),
                Listcount = listcount,
            };
        }



        /// <summary>
        /// 报损批量删除 
        /// </summary>entity
        /// <param name="RoldId"></param>
        /// <returns></returns>
        [HttpDelete]
    public async Task<ResultDto> DeleRoleAsync(string BId)
        {
            if (BId != null)
            {
                string[] ids = BId.Split(',');
                foreach (var item in ids)
                {
                    await db.DeleAsync(Convert.ToInt64(item));
                }
                return new ResultDto { Code = Status.Success, Message = "删除成功" };
            }
            else
            {
                return new ResultDto { Code = Status.Success, Message = "删除失败" };
            }

        }







        /// <summary>
        /// 报损修改
        /// </summary>
        /// <param name="RoleInputDTo"></param>
        /// <returns></returns>

        [HttpPut]
        public async Task<ResultDto> RoleUpdateAsync(breakage breakage)
        {
            try
            {
                await db.UpdateAsync(breakage);
                return new ResultDto { Code = Status.Success, Message = "修改成功" };

            }
            catch (Exception e)
            {
                return new ResultDto { Code = Status.Failed, Message = e.Message };
            }
        }


        /// <summary>
        /// 报损反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet]
        public async Task<ResultDto<breakage>> BackFill(long id)
        {
            var list = await db.FistAsync(x => x.BId == id);

            return new ResultDto<breakage> { Code = Status.Success, Message = "成功", Data = list };
        }
    }
}
